<template>
  <el-aside :width="isCollapse ? '64px':'200px'">
    <div class="btn_trigger" @click="toggleCollapse">|||</div>
    <el-menu
      :collapse="isCollapse"
      :collapse-transition="false"
      unique-opened
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409eff"
      :router="true"
      :default-active="activePath"
    >
      <el-submenu :index="'/'+item.path" v-for="(item,index) in menuList" :key="item.id">
        <template slot="title">
          <i :class="`iconfont icon-${iconsList[index]}`"></i>
          <span>{{item.authName}}</span>
        </template>

        <el-menu-item
          :index="'/'+item2.path"
          v-for="item2 in item.children"
          :key="item2.id"
          @click="handleClick('/'+item2.path)"
        >
          <template slot="title">
            <i class="el-icon-location"></i>
            <span>{{item2.authName}}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
  </el-aside>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    console.log(this.activePath)
  },
  data() {
    return {
      menuList: [],
      activePath: '',
      isCollapse: false,
      iconsList: ['users', 'tijikongjian', 'shangpin', 'danju', 'baobiao']
    }
  },
  methods: {
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    handleClick(path) {
      this.activePath = path
      window.sessionStorage.setItem('activePath', path)
      console.log(this.activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.el-aside {
  background-color: #333744;
}
.btn_trigger {
  background-color: #3a5064;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  font-size: 10px;
  line-height: 24px;
  //   padding: 5px 0;
  letter-spacing: 0.2em;
}
.el-menu {
  border-right: none;
}
.iconfont {
  margin-right: 10px;
}
</style>